<template>
  <!-- 订单发货页面 -->
  <el-dialog :visible.sync="visible" title="订单发货" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="orderStatus" label="物流模式">
        <ren-radio-group v-model="dataForm.logisticsType" dict-type="logistics_type" placeholder="请选择物流模式"></ren-radio-group>
      </el-form-item>
      <el-form-item prop="expressCompany" label="物流公司">
        <el-select v-model="dataForm.expressCompany" filterable clearable placeholder="请选择物流公司">
          <el-option v-for="item in expressCompanyList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="trackingNo" label="物流单号">
        <el-input v-model="dataForm.trackingNo" placeholder="请输入物流单号" clearable></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import {getDictDataList} from "@/utils";

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist:{},
      expressCompanyList:[],                //所有物流公司信息
      dataForm: {
        id: '',                         //订单id
        userId:'',                      //用户id
        orderStatus: 2,                 //订单的当前状态，(1) 待发货；(2) 已发货；
        logisticsType:1,                //物流模式
        expressCompany:'',              //物流公司编码，快递公司ID
        trackingNo:'',                  //物流单号，物流快递发货时必填，示例值: 323244567777 字符字节限制: [1, 128]
      }
    }
  },
  computed: {
    dataRule() {
      return {
        logisticsType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        expressCompany: [
          {required: this.dataForm.logisticsType==='3'||this.dataForm.logisticsType==='4'?false: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        trackingNo: [
          {required: this.dataForm.logisticsType==='3'||this.dataForm.logisticsType==='4'?false: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
          this.userOrderDelivery()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userOrder/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 获取运力信息
    userOrderDelivery() {
      this.expressCompanyList = getDictDataList('delivery_list')
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        //console.log(this.dataForm)
        if (!valid) {
          return false
        }
        this.$http['put']('/api/userPointsOrder', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
